package com.ocom.human.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.human.EntityPersonTag;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.human.dao.IDaoPersonTag;
import com.ocom.human.service.PersonTagService;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PersonTagServiceImpl extends ServiceImpl<IDaoPersonTag, EntityPersonTag> implements PersonTagService {


	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId")
	public List<EntityPersonTag> list(Long comId, Integer state, Integer groupNo) {
		QueryWrapper<EntityPersonTag> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("com_id", comId);
		if (state == 1) {
			queryWrapper.eq("state", state);
		}
		if (groupNo != 0) {
			queryWrapper.eq("group_no", groupNo);
		}
		return this.list(queryWrapper);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId")
	public Integer addPersonTag(Long comId, EntityPersonTag entityPersonTag) {
		QueryWrapper<EntityPersonTag> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("com_id", comId);
		queryWrapper.eq("tag_name", entityPersonTag.getTagName());
		queryWrapper.last("limit 1");
		Integer co = this.count(queryWrapper);
		if (co > 0) return -1;

		entityPersonTag.setState(1);
		Boolean bo = this.save(entityPersonTag);
		return bo == true ? 1 : 0;
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId")
	public Integer updatePersonTag(Long comId, EntityPersonTag entityPersonTag) {
		QueryWrapper<EntityPersonTag> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("com_id", comId);
		queryWrapper.eq("tag_name", entityPersonTag.getTagName());
		queryWrapper.ne("tag_id", entityPersonTag.getTagId());
		queryWrapper.last("limit 1");
		Integer co = this.count(queryWrapper);
		if (co > 0) return -1;

		UpdateWrapper<EntityPersonTag> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("com_id", comId);
		updateWrapper.eq("tag_id", entityPersonTag.getTagId());

		updateWrapper.set("tag_name", entityPersonTag.getTagName());
		updateWrapper.set("state", entityPersonTag.getState());
		updateWrapper.set("remark", entityPersonTag.getRemark());
		updateWrapper.set("group_no", entityPersonTag.getGroupNo());

		Boolean bo =  this.update(updateWrapper);

		return bo == true ? 1 : 0;

	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId")
	public int countPersonTag(Long comId, Integer groupNo) {
		QueryWrapper<EntityPersonTag> queryWrapper = Wrappers.query();
		queryWrapper.eq("com_id", comId);
		queryWrapper.eq("group_no", groupNo);
		return this.count(queryWrapper);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId")
	public boolean removePerTagGroup(Long comId, Integer groupNo, String remark) {
		UpdateWrapper<EntityPersonTag> updateWrapper = Wrappers.update();
		updateWrapper.eq("com_id", comId);
		updateWrapper.eq("group_no", groupNo);

		updateWrapper.set("group_no", 0);
		updateWrapper.set("remark", remark);
		updateWrapper.set("update_time", new Date());
		return this.update(updateWrapper);
	}
}
